package com.supermap.dataservice.meto.life;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.XmlUtil;
import com.supermap.dataservice.webservice.Axis2Client;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;




/**
 * @author aqua
 * @description 生活指数
 * @date 2020年04月15 12:04
 * @email 1301584033@qq.com
 */
@Component
@EnableScheduling
public class LifeIndicatorsTask {

    @Resource
    private LifeMapper lifeMapper;
    @Scheduled(cron = "0 0 15 * * ?")
    public void init(){
        String os = System.getProperty("os.name");
        System.out.println("推送数据"+"当前操作系统:"+os+"----------------------------------------------");
        if(os.toLowerCase().indexOf("windows")!=-1){
            System.out.println("判断为操作开发环境不采集生活指标，已退出！");
            return;
        }
        List<String> strings=null;
        Calendar cal = Calendar.getInstance();
        int HH=Integer.valueOf(DateUtil.format(DateUtil.date(),"HH"));
        if(HH>=15){
            cal.add(Calendar.DATE,1);
        }
        String dateDay= DateUtil.format(DateUtil.date(cal),"yyyyMMdd");
        String xmlResult= XmlUtil.cleanInvalid(Axis2Client.zhishu(dateDay));
        SAXReader reader = new SAXReader();
        List<LifeMode> listModel= new ArrayList();
        try {
            org.dom4j.Document doc = reader.read(new ByteArrayInputStream(xmlResult
                    .getBytes("UTF-8")));
            org.dom4j.Element root = doc.getRootElement();
            org.dom4j.Element element;
            org.dom4j.Element element2;
            for (Iterator i = root.elementIterator("CityCode"); i.hasNext();) {
                LifeMode model=new LifeMode();
                element = (org.dom4j.Element) i.next();
                model.setCityCode(element.attributeValue("CityCode").trim());
                for (Iterator j = element.elementIterator("forecastTime"); j
                        .hasNext();) {
                    element2 = (org.dom4j.Element) j.next();
                    model.setForecastTime(element2.attributeValue("forecastTime").trim());
                    model.setWeatherDescription(element2.elementText("weatherDescription"));
                    model.setDryingIndeX(element2.elementText("DryingIndeX"));
                    model.setCityFireIndeX(element2.elementText("CityFireIndeX"));
                    model.setWishingCarIndeX(element2.elementText("wishingCarIndeX"));
                    model.setWarehouseFireIndeX(element2.elementText("WarehouseFireIndeX"));
                    model.setDrinkIndex(element2.elementText("drinkIndex"));
                    model.setDressIndex(element2.elementText("dressIndex"));
                    model.setFeverIndex(element2.elementText("feverIndex"));
                    model.setTourIndex(element2.elementText("tourIndex"));
                    model.setMorningExecIndex(element2.elementText("morningExecIndex"));
                    model.setCozyIndex(element2.elementText("cozyIndex"));
                    model.setHumidityIndex(element2.elementText("humidityIndex"));
                    model.setDriveIndex(element2.elementText("driveIndex"));
                    model.setBeerIndex(element2.elementText("beerIndex"));
                    model.setMakeupIndex(element2.elementText("MakeupIndex"));
                    model.setLookIndex(element2.elementText("LookIndex"));
                    listModel.add(model);
                }

            }
            lifeMapper.insertLifeSQL(listModel);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}
